package edu.tfsu.team_2208.skylarkim.service;

import jakarta.annotation.PostConstruct;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Service
@Log
public class HdfsServiceLiu {

    @Value("${Liu.hdfs.path}")
    private String path;

    @Value("skylarkim")
    private String username;

    private static String hdfsPath = "hdfs://192.168.213.129:8020";
    private static String hdfsName = "skylarkim";

    private static final int bufferSize = 1024 * 1024 * 64;

    /**
     * 获取HDFS配置
     * @return
     */
    private static Configuration getConfiguration() {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", hdfsPath);
        return conf;
    }

    /**
     * 获取HDFS文件系统
     * @return
     */
    public static FileSystem getFileSystem() {
        try {
            FileSystem fs = FileSystem.get(new URI(hdfsPath), getConfiguration(), hdfsName);
            return fs;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 判断文件是否存在
     * @param path
     * @return
     */
    public static boolean existFile(String path) {
        if (StringUtils.isEmpty(path)) {
            return false;
        }

        try {
            FileSystem fs = getFileSystem();
            Path srcPath = new Path(path);
            if (fs.exists(srcPath)) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 读取文件
     * @param path
     * @return
     */
    public static List<String> readFile(String path,int start,int limit) {
        if (!existFile(path)) {
            return null;
        }

        List<String> lines = new ArrayList<>();
        FileSystem fs = getFileSystem();
        Path srcPath = new Path(path);
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(fs.open(srcPath)));
            String line;
            int count = 0;
            while ((line = reader.readLine()) != null && lines.size() < limit) {
                count++;
                if (count <= start) {
                    continue;
                }
                lines.add(line.replace("\t",  ",")
                        .replace( "\n",  ""));
            }
            reader.close();
            fs.close();
            return lines;
        } catch (IOException e) {
            e.printStackTrace();
            log.warning( "Hadoop文件读取错误");
        }

        return null;
    }


    /**
     * 获取HDFS路径
     */
    @PostConstruct
    public void getPath() {
        hdfsPath = "hdfs://192.168.213.129:8020";
    }

    /**
     * 获取HDFS用户名
     */
    @PostConstruct
    public void getName() {

        hdfsName = "skylarkim";
    }

    public static String getHadoopPath() {
        return hdfsPath;
    }

    public static String getHadoopName() {
        return hdfsName;
    }


}